<?php
class news extends CI_Controller
{
  protected $_templates;
  
  
  function __construct()
  {
	  parent::__construct();
	  
	  $this->pre_message = "";
	  $this->load->model('news_model','news');
	  $this->load->library('editor_library');
	  $this->load->library('icon_library');
	  $this->load->helpers('str_helper');
	  $this->load->helpers('img_helper');
  }
  
  function index()
  {
	  $this->listnews();
  }
  
    // Bengin list news
  function listnews()
  {
	  $data['title'] = 'Danh sách bài viết';
	  $config['base_url'] = base_url().'index.php/news/listnews/'.(int)$this->uri->segment(3); 
	  $data['add'] = 'news/addnews';//trang add trong news/list/add
 
	  //pagging
	  $data['num'] = $this->news->getNumNews();
	  $config['total_rows']   =  $data['num'];
	  $config['per_page']  =   '20';
	  $config['uri_segment'] = 4;   
	  $this->pagination->initialize($config);   
	  $data['list'] =   $this->news->getAllNews($config['per_page'],(int)$this->uri->segment('4'));
	  $data['pagination']    = $this->pagination->create_links();            
	  $this->_templates['page'] = 'news/list/index';
	  $this->site->load($this->_templates['page'],$data);
 
  }
  
  function addnews()
  {
	  $data['title'] = 'Thêm mới bài viết';
	 
	  $this->form_validation->set_rules('ContentTitle','Tiêu đề','trim|required');
	  $this->form_validation->set_rules('ContentImg','','');
	  $this->form_validation->set_rules('ContentSmall','','');
	  $this->form_validation->set_rules('ContentBody','','');
	  if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
	  }else{
		  if($this->news->savenews()){			 
		  	if($this->input->post('ContentImg')!=''){
				 if(file_exists('../uploads/news/'.$this->input->post('ContentImg'))){
					 unlink('../uploads/news/'.$this->input->post('ContentImg'));
				 }
				fn_resize_image('../uploads/images/'.$this->input->post('ContentImg'),'../uploads/news/'.$this->input->post('ContentImg'),160,120); 
			  }                      
			  $this->session->set_flashdata('message','Lưu thành công');
			  redirect('news/listnews');
		  }
	  }

	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'news/list/add';
	  $this->site->load($this->_templates['page'],$data);
  }

	function updatenews()
	{
		$id = (int)$this->uri->segment(3);
		$data['rs'] = $this->news->getItemNews($id);
		$data['title'] = 'Cập nhật: '.$data['rs']->title_vn;
		
	  $this->form_validation->set_rules('ContentTitle','Tiêu đề','trim|required');
	  $this->form_validation->set_rules('ContentImg','','');
	  $this->form_validation->set_rules('ContentSmall','','');
	  $this->form_validation->set_rules('ContentBody','','');
	  if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
	  }else{
		  if($this->news->savenews()){			 
		  	if($this->input->post('ContentImg')!=''){
				 if(file_exists('../uploads/news/'.$this->input->post('ContentImg'))){
					 unlink('../uploads/news/'.$this->input->post('ContentImg'));
				 }
				fn_resize_image('../uploads/images/'.$this->input->post('ContentImg'),'../uploads/news/'.$this->input->post('ContentImg'),160,120); 
			  }                      
			  $this->session->set_flashdata('message','Lưu thành công');
			  redirect('news/listnews');
		  }
	  }

	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'news/list/update';
	  $this->site->load($this->_templates['page'],$data);
	}
	
	function del()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		
		//	if($this->news->del_news($id))
		if($this->news->delete($id)){
			$this->session->set_flashdata('message','Xóa thành công');
			redirect('news/listnews/0/'.$page);
		}
		else $this->session->set_flashdata('message','Xóa không thành công');
		
	}
	
	function delsnews()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						//del_updates($id)
						if($this->news->delete($ar_id[$i]))
						//if($this->news->del_news($ar_id[$i]))
						$this->session->set_flashdata('message','Xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('news/listnews/0/'.$page);
	}    

}
?>
